/*
 * (c) rdrrlabs gmail com, 2011
 * Project: 24ClockWidget
 * License GPLv3
 */

package com.rdrrlabs.a24clock.prefs;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;

import com.alfray.a24clock.R;

//-----------------------------------------------

public class StartActivityPreference extends Preference {

    public static final String TAG = StartActivityPreference.class.getSimpleName();

    public static final String EXTRA_BOOL_USE_DIALOG_THEME = "use_dialog_theme";


    private String mActivityName;

    public StartActivityPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initAttrs(context, attrs);
    }

    public StartActivityPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
    }

    public StartActivityPreference(Context context) {
        super(context);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StartActivityPref);
        if (a != null) {
            mActivityName = a.getString(R.styleable.StartActivityPref_activityName);
        }
    }

    @Override
    protected void onClick() {
        super.onClick();
        if (mActivityName != null) {
            Intent i = new Intent();
            ComponentName cn = new ComponentName(getContext(), mActivityName);
            i.setComponent(cn);
            i.putExtra(EXTRA_BOOL_USE_DIALOG_THEME, true);
            try {
                getContext().startActivity(i);
            } catch(ActivityNotFoundException e) {
                Log.e(TAG, "Failed to start activity" , e);
            }
        }
    }
}
